﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;
namespace AutoChess
{
    public class AutoChessSceneLoadComponent
    {
        private int mapId;
        private int sceneClientId = -1;
        private bool isSceneLoadComplete = false;
        private string sceneResName = string.Empty;
        private Table.DataModel.TableSceneClient sceneClient;

        public bool IsSceneLoadComplete
        {
            get { return isSceneLoadComplete; }
        }

        public void LoadCurPlayerScene()
        {
            isSceneLoadComplete = false;
            if (IsNeedLoadScene())
            {
                sceneClientId = AutoChessManager.Ins.cacheData.CurPlayerData.SceneClientId;
                AssetManager.Instance.StartCoroutine(LoadScene());
            }
            else
            {
                isSceneLoadComplete = true;
            }
        }

        public bool IsNeedLoadScene()
        {
            if (sceneClientId != AutoChessManager.Ins.cacheData.CurPlayerData.SceneClientId)
            {
                return true;
            }
            return false;
        }

        private IEnumerator LoadScene()
        {
            sceneClient = Table.Blo.TableSceneBlo.GetTableSceneClient(sceneClientId);
            isSceneLoadComplete = false;
            sceneResName = "maps/" + sceneClient.sceneResName;
            yield return AssetManager.Instance.LoadSceneAsync(sceneResName);
            isSceneLoadComplete = true;
        }

        public void PlayCurSceneAudio()
        {
            AudioTool.Instance.StopBackgroundMusic();
            AudioTool.Instance.LoadAndPlayBackgroundMusic(sceneClient.bgSound);
            AutoChessFieldViewCtr.Ins.SceneConfig.backAudio = sceneClient.bgSound;
        }
    }
}
